var app = angular.module("app.directives", []);

app.directive("highlight", ['$location',function ($location) {
    return {
        restrict: "A",
        link: function (scope, ele, attrs) {
            /*
            var path = attrs.highlight;

            var highlight = function () {
                ele.removeClass('highlight');

                if($location.path() == path) {
                    if(!ele.hasClass('highlight')) {
                        ele.addClass('highlight');
                    }
                    if(!ele.hasClass('treeview')) {
                        var treeviw = ele.parent().parent();
                        treeviw.addClass('highlight');
                    }
                } else {
                    ele.removeClass('highlight');
                }
            };

            //首先高亮一次
            //highlight();

            //页面跳转之后再重新高亮
            scope.$on("$locationChangeSuccess", function (event, data) {
                highlight();
            });
            */
        }
    }
}]);


app.directive("sidebarMenu", ['$location',function ($location) {
    return {
        restrict: "A",
        link: function (scope, ele, attrs) {
            var highlight = function () {
                $(".sidebar-menu li").removeClass("highlight");

                $(".sidebar-menu a").each(function() {
                    var hrefs = $(this).attr("href").split("#");
                    if(hrefs.length < 2) {
                        return;
                    }
                    if(hrefs[1] == $location.path()) {
                        var parentLi = $(this).parent();
                        parentLi.addClass("highlight");
                        if(!parentLi.hasClass("treeview")) {
                            var rootLi = parentLi.parents(".treeview");
                            rootLi.addClass("highlight");
                        }
                    }
                });
            };

            //首先高亮一次
            //highlight();

            //页面跳转之后再重新高亮
            scope.$on("$locationChangeSuccess", function (event, data) {
                highlight();
            });
        }
    }
}]);


